import router from '@/router/index.js'

const asyncRouter = {
  /**
   * 添加路由
   * @param router
   */
  loadRouter: (menus) => {
    if (menus.length >= 0) {
      for (let i = 0; i < menus.length; i++) {
        if (menus[i].children.length > 0) {
          this.loadRouter(menus[i].children)
        } else {
          this.addRouter(menus[i].resourceUrl)
        }

      }
    }
  },
  hasName: (name) => {
    let has = false
    router.options.routes[0].children.forEach((it) => {
      if (it.path == name) {
        has = true
      }
    })
    return has
  },
  addRouter: (path) => {
    let has = false
    router.options.routes[0].children.forEach((it) => {
      if (it.path == path) {
        has = true
      }
    })

    if (!has) {
      let routePath = path.split('?')[0]
      router.options.routes[0].children.push({
        path: `${routePath}`,
        component: () => import( '@/pages/' + routePath.replace('/pages/', '')),
      })
      router.addRoutes(router.options.routes)
      console.log(router)
    }
  }

}

export {asyncRouter}
